package net.mikoo.seals.biz.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.mikoo.seals.biz.mapper.ApplyMapper;
import net.mikoo.seals.biz.mapper.DevicesActMapper;
import net.mikoo.seals.biz.mapper.DevicesUsedCertMapper;
import net.mikoo.seals.biz.mapper.DevicesUsedFailMapper;
import net.mikoo.seals.biz.mapper.PersonMapper;
import net.mikoo.seals.biz.service.DevicesActService;
import net.mikoo.seals.common.enums.CensorStatusEnum;
import net.mikoo.seals.common.exception.ParametersException;
import net.mikoo.seals.common.model.PageModel;
import net.mikoo.seals.model.Apply;
import net.mikoo.seals.model.DevicesAct;
import net.mikoo.seals.model.DevicesActQuery;
import net.mikoo.seals.model.Upfile;

@Service
public class DevicesActServiceImpl implements DevicesActService {

	@Autowired
	private DevicesActMapper devicesActMapper;
	
	@Autowired
	private DevicesUsedCertMapper devicesUsedCertMapper;
	
	@Autowired
	private ApplyMapper applyMapper;
	
	@Autowired
	private PersonMapper personMapper;

	@Autowired
	private DevicesUsedFailMapper devicesUsedFailMapper;
	
	@Override
	public PageModel<DevicesAct> getActPageByQuery(DevicesActQuery query) {
		List<DevicesAct> datas = devicesActMapper.selectPageByQuery(query);
		Integer total = devicesActMapper.countPageByQuery(query);
		PageModel<DevicesAct> pm = new PageModel<>(total, datas);
		
		return pm;
	}
	
	@Override
	public List<DevicesAct> getListByQuery(DevicesActQuery query) {
		
		if(query == null) {
			throw new ParametersException("query is null");
		}
		
		return devicesActMapper.selectListByQuery(query);
	}

	@Override
	public DevicesAct getInfoById(Long devicesActId) {
		if(devicesActId == null) {
			throw new ParametersException("devicesActId is null");
		}
		
		DevicesAct act = devicesActMapper.selectInfoById(devicesActId);
		if(act != null) {
			act.setUpfiles(devicesUsedCertMapper.selectUpfileByActId(act.getDevicesActId()));
			Apply apply = applyMapper.selectInfoById(act.getApplyId());
			if(apply != null) {
				List<Upfile> voucherList = applyMapper.selectApplyfiles(act.getApplyId());
				apply.setUpfiles(voucherList);
			}
			act.setApply(apply);
			
			if(act.getCensorPartyId() != null) {
				act.setCensorPerson(personMapper.selectByPartyId(act.getCensorPartyId()));
			}
			
			Integer failTimes = devicesUsedFailMapper.selectFailTimes(devicesActId);
			act.setFailTimes(failTimes == null ? 0 : failTimes);
		}
		return act;
	}

	@Override
	public DevicesAct getInfoByQuery(DevicesActQuery query) {
		if(query == null) {
			throw new ParametersException("query is null");
		}
		if(query.getDevicesActId() == null) {
			throw new ParametersException("DevicesActId is null");
		}
		if(query.getCompanyId() == null) {
			throw new ParametersException("Company is null");
		}
		
		DevicesAct act = devicesActMapper.selectInfoByQuery(query);
		
		return act;
	}
	
	@Override
	public void censor(DevicesAct devicesAct) {
		if(devicesAct == null) {
			throw new ParametersException("devicesUsed is null");
		}
		
		if(devicesAct.getCompanyId() == null) {
			throw new ParametersException("CompanyId is null");
		}
		
		if(devicesAct.getDevicesActId() == null) {
			throw new ParametersException("DevicesActId is null");
		}
		
		if(devicesAct.getCensorPartyId() == null) {
			throw new ParametersException("CensorPartyId is null");
		}
		
		DevicesAct oldAct = devicesActMapper.selectInfoById(devicesAct.getDevicesActId());
		if(oldAct == null) {
			return ;
		}
		
		oldAct.setCensorPartyId(devicesAct.getCensorPartyId());
		oldAct.setCensorStatus(CensorStatusEnum.CHECKED.getCode());
		oldAct.setCensorTime(new Date());
		devicesActMapper.update(oldAct);
	}
}
